import 'dart:async';
import 'dart:convert';

import 'package:app/common/abs_view.dart';
import 'package:app/model/user/login_user_info.dart';
import 'package:app/ui/dialog/login_dialog.dart';
import 'package:app/utils/event_bus_utils.dart';
import 'package:app/utils/user_utils.dart';
import 'package:flutter/material.dart';

class UserCenterView extends StatefulWidget {
  const UserCenterView({super.key});

  @override
  State<StatefulWidget> createState() {
    return UserCenterViewState();
  }
}

class UserCenterViewState extends State<UserCenterView> {
  LoginUserInfo? loginUserInfo = UserStates().getLoginUserInfo();
  late StreamSubscription _eventBusFn;
  @override
  void initState() {
    super.initState();
    _eventBusFn =
        GEventBusUtils().getDefault().on<LoginUserInfo>().listen((event) {
      setState(() {
        loginUserInfo = event;
      });
    });
  }

  @override
  void dispose() {
    super.dispose();
    //取消订阅
    _eventBusFn.cancel();
  }

  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        ProfilePanel(this).getView(context),
        RigthPanel(this).getView(context),
      ],
    );
  }
}

class ProfilePanel extends AbsView {
  UserCenterViewState _state;
  ProfilePanel(this._state);
  @override
  Widget getView(BuildContext context) {
    bool isLogin = UserStates().isLogin;
    return SizedBox(
      width: 280,
      height: double.infinity,
      child: Card(
          margin: const EdgeInsets.all(12),
          elevation: 18,
          child: Column(
            children: [
              Container(
                margin: const EdgeInsets.fromLTRB(12, 38, 12, 0),
                child: ClipOval(
                  child: isLogin
                      ? Image.network(
                          _state.loginUserInfo!.headicon ?? "",
                          width: 120,
                          height: 120,
                          fit: BoxFit.cover,
                        )
                      : Image.asset("assets/images/user_default_icon.png",
                          width: 120, height: 120, fit: BoxFit.cover),
                ),
              ),
              Container(
                margin: const EdgeInsets.all(12),
                child: Text(isLogin
                    ? _state.loginUserInfo!.nickName ?? "昵称为空"
                    : "尚未登陆"),
              ),
              Container(
                margin: const EdgeInsets.fromLTRB(12, 0, 12, 0),
                child: Text(isLogin
                    ? _state.loginUserInfo!.motto ?? "这家伙很懒，什么也没有留下"
                    : "-"),
              )
            ],
          )),
    );
  }
}

class RigthPanel extends AbsView {
  UserCenterViewState _state;
  RigthPanel(this._state);
  @override
  Widget getView(BuildContext context) {
    bool isLogin = UserStates().isLogin;
    return Flexible(
      flex: 1,
      child: SizedBox(
        height: double.infinity,
        width: double.infinity,
        child: Card(
          margin: const EdgeInsets.fromLTRB(0, 12, 12, 12),
          elevation: 18,
          child: Stack(
            children: [
              (isLogin
                  ? Image.network(
                      _state.loginUserInfo?.cover ?? "",
                      fit: BoxFit.fill,
                    )
                  : const Positioned(child: LoginDialog()))
            ],
          ),
        ),
      ),
    );
  }
}

class LoginUserCenterOperateView extends AbsView {
  UserCenterViewState _state;
  LoginUserCenterOperateView(this._state);

  @override
  Widget getView(BuildContext context) {
    return const Center();
  }
}
